package meijuli.com.join51.business.ui;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import meijuli.com.join51.business.R;


/**
 * 请描述使用该类使用方法！！！
 *
 * @author wenqin 2017-04-24 14:09
 */

public class ListDialog {

    public static Dialog showDialog(Context context, String[] data,
                                  final OnListItemClickListener listener) {
        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
        View convertView = View.inflate(context, R.layout.dialog_listview, null);
        alertDialog.setView(convertView);
        ListView lv = (ListView) convertView.findViewById(R.id.list_view);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(context,
                android.R.layout.simple_list_item_1, data);
        lv.setAdapter(adapter);
        final Dialog dialog = alertDialog.create();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dialog.dismiss();
                if (listener != null) {
                    listener.listItemClick(position);
                }
            }
        });
        dialog.show();
        return dialog;
    }

    public interface OnListItemClickListener {

        void listItemClick(int position);
    }
}
